<html>

<head>
    <?php
    session_start();
    include '../tools/tools.php';

    class Q_priview
    {
        function __construct($No, $question, $ans1, $ans2, $ans3, $ans4, $author, $bingo, $withpic, $pic, $label)
        {
            $this->No = $No;
            $this->question = $question;
            $this->ans = array($ans1, $ans2, $ans3, $ans4);
            $this->ans_id = array(1, 2, 3, 4);
            $this->author = $author;
            $this->bingo = (int)$bingo;
            if ($withpic == '1') {
                $this->withpic = true;
                $this->pic = $pic;
            }
            $this->label = $label;
        }
    }

    function generate_Q_priv()
    {
        $db = local_db();
        $sheet = 'questions_con';
        $sql = "SELECT * FROM `$sheet` WHERE checked='0'";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            $ques = new Q_priview($row['No'],$row['q'], $row['ans1'], $row['ans2'], $row['ans3'], $row['ans4'], 
                                    $row['author'], $row['bingo'], $row['withpic'], $row['pic_addr'], $row['label']);
        } else {
            $ques = false;
        }
        return $ques;
    }
    echo '
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
    <script>
            function logout_trigger(){
                var form = document.getElementById(\'logout_\'),
                    formData = new FormData(form);
                $.ajax({
                    url: "login.php",
                    type: "post",
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (res) {
                        location.reload();
                    },
                    error: function (err) {
                        alert("退出登录失败", err);
                    }
                });
            }
            function login_trigger(){
                var form = document.getElementById(\'login_\'),
                    formData = new FormData(form);
                $.ajax({
                    url: "login.php",
                    type: "post",
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (res) {
                        location.reload();
                    },
                    error: function (err) {
                        alert("登录失败", err);
                    }
                });
            }
            function check(cmd){
                    formData = new FormData();
                    formData.append(\'cmd\',cmd);
                $.ajax({
                    url: "db.php",
                    type: "post",
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (res) {
                        location.reload();
                    },
                    error: function (err) {
                        alert("后台错误", err);
                    }
                });
            }
    </script>
    <style>
    img{
        max-width: 300px;
        min-height: 100px;
    }
    button{
        width: 100px;
    }
    </style>
    ';
    ?>
</head>

<body>

    <?php
    if ($_SESSION['logged_in']) {
        echo '你好，' . $_SESSION['username'];
        $ques = generate_Q_priv();
        if (!$ques) {
            echo '<br>太好啦~已经全部审核完成~';
            unset($_SESSION['ques_No']);
        }
        else {
            $_SESSION['ques_No']=$ques->No;
            $replacer = array('A', 'B', 'C', 'D');
            echo '<div id="" class="mainDiv">';
            echo '<div id="q" class="contentDiv">';
            echo "<br>题目：";
            echo '<div id="question" class="question">', $ques->question, '</div>';
            if($ques->withpic){
                echo '<img src="'.$ques->pic.'" alt="图片加载中"></img><br>';
            }
            for ($choice = 0; $choice <= 3; $choice++) {
                echo '<input type="radio" name="q_" value="', $ques->ans_id[$choice], '" id="ans_', $ques->ans_id[$choice], '" ';
                if($choice == $ques->bingo-1){
                    echo 'checked';
                }
                echo '/><label for="ans_', $ques->ans_id[$choice], '">', $replacer[$choice], '. ', $ques->ans[$choice], '</label>';
                echo '<br>';
            }
            echo '<div>正确答案：'.$replacer[$ques->bingo-1],'</div>';
            echo '<div>出题人：'.$ques->author,'</div>';
            echo '<div>标签：'.$ques->label,'</div>';
            echo '</div>';
            echo '</div>';
            
            //入库
            echo '<button onclick="check(\'easy\')">加入入站题库</button>';
            echo '<button onclick="check(\'hard\')">加入高难题库</button><br>';
            //echo '<button onclick="check(\'both\')">加入双题库</button>';
            echo '<button onclick="check(\'bad\')">这啥破题啊</button>';
            
        }
        echo '
        <form action="login.php" target="_parent" method="POST" id="logout_">
        <input name="logout" value="true" style = "display:none" form="logout_">
            <br><button onclick="logout_trigger()">退出登录</button>
        </form>
        ';
    } else {
        echo '
    <form action="login.php" target="_parent" method="POST" id="login_">
        用户名： <input type="text" name="username"><br>
        密  码： <input type="text" name="password"><br>
        <button onclick="login_trigger()">登录</button>
    </form>
    ';
    }
    ?>

</body>